package com.thank.security.browser;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * description: none
 *
 * @author thank
 * 2018/2/21 16:50
 */
@Component
public class MyUserDetailService implements UserDetailsService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private PasswordEncoder passwordEncoder;

    // 这里可以注入jpa, mybatis等查询数据库
//    private UserDao userDao;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        logger.info("登录用户名: " + username);
        // 根据用户名查找用户信息
        // AuthorityUtils.commaSeparatedStringToAuthorityList("admin") 将字符串转成GrantedAuthority对象集合
//        User user = new User(username, "123456", AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));

        // 除了用户名和密码, 权限, 还有几个用户有效性的验证
        String password = passwordEncoder.encode("123456");
        logger.info("数据库密码: " + password);
        User user = new User(username, password,
                true, true, true, true,
                AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
        return user;
    }
}
